package com.shoulder.feign.utils;

import com.shoulder.core.api.ApiResult;
import com.shoulder.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@Slf4j
public class ClientUtils {

    public static <R> ApiResult<R> invoke(Supplier<ApiResult<R>> func) {
        try {
            return func.get();
        } catch (Exception e) {
            log.error("feign调用[Supplier<ApiResult<R>> func]异常:{}", e.getMessage());
            throw new BizException("feign调用异常");
        }
    }

    public static <R, T> ApiResult<R> invoke(T t, Function<T, ApiResult<R>> func) {
        try {
            return func.apply(t);
        } catch (Exception e) {
            log.error("feign调用[Function<T, ApiResult<R>> func]异常:{}", e.getMessage());
            throw new BizException("feign调用异常");
        }
    }


    public static <T> ApiResult<Void> invoke(T t, Consumer<T> func) {
        try {
            func.accept(t);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error("feign调用[Consumer<T> func]异常:{}", e.getMessage());
            throw new BizException("feign调用异常");
        }
    }
}
